package com.geostar.constructionland.common.service.logs.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geostar.constructionland.common.entity.logs.JsydFxrz;
import com.geostar.constructionland.common.entity.logs.JsydFxrzLs;
import com.geostar.constructionland.common.global.ConstAnalyseType;
import com.geostar.constructionland.common.mapper.logs.JsydFxrzLsMapper;
import com.geostar.constructionland.common.mapper.logs.JsydFxrzMaper;
import com.geostar.constructionland.common.service.logs.JsydFxrzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class JsydFxrzServiceImpl extends ServiceImpl<JsydFxrzMaper, JsydFxrz> implements JsydFxrzService {
    @Autowired
    JsydFxrzLsMapper jsydFxrzLsMapper;
    @Autowired
    JsydFxrzMaper jsydFxrzMaper;

    @Override
    public void saveJsydFxrz(String ywid, List<JsydFxrz> jsydFxrzs) {
        List<String> fxlxStrs = jsydFxrzs.stream().map(JsydFxrz::getFxlx).collect(Collectors.toList());
        /**获取分析历史数据*/
        List<JsydFxrz> jsydFxrz_ls = this.baseMapper.
                selectList(new QueryWrapper<JsydFxrz>().eq("ywid", ywid).in("fxlx", fxlxStrs));
        /**转存相关的占压数据至历史表中*/
        jsydFxrz_ls.stream().forEach(targetFxrz -> {
            JsydFxrzLs jsydFxrzLs = new JsydFxrzLs(targetFxrz);
            jsydFxrzLsMapper.insert(jsydFxrzLs);
            this.baseMapper.deleteById(targetFxrz.getFxrzid());
        });
        this.saveBatch(jsydFxrzs);
    }
    @Override
    public JsydFxrz getFxrzByYwidAndFxlx(String ywid, String fxlx) {
        if (ConstAnalyseType.CONST_CDZDFX.equals(fxlx)) {
            fxlx = ConstAnalyseType.CONST_CDZDFX + "_BAK";
        }

        JsydFxrz fxrz = jsydFxrzMaper.selectList(new QueryWrapper<JsydFxrz>().lambda()
                .eq(JsydFxrz::getFxlx, fxlx)
                .eq(JsydFxrz::getYwid, ywid)
                .orderByDesc(JsydFxrz::getFxsj))
                .stream().findFirst().orElse(null);
        return fxrz;
    }
}
